package com.c1g19.dao.office;

import com.c1g19.dao.BaseDao;
import com.c1g19.utils.BasicRowProcessorFix;
import com.c1g19.utils.DbControl;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.apache.log4j.Logger;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 该类为科室类Dao层，用以查询与科室相关的内容
 * Created by Mr_C on 2017/3/7.
 */
public class OfficeDao extends BaseDao{

    private static final Logger logger = Logger.getLogger(OfficeDao.class);

    /**
     * 得到所有的科室类
     * @return 所有科室的MapList
     */
    public Map<String, Object> getAllOffice(){
        logger.info("执行 getAllOffice");
        Map<String, Object> dataMap = new HashMap<String, Object>();
        List<Map<String, Object>> officeMapList = new ArrayList<Map<String, Object>>(); //诊室的list
        long totalCount = 0; // 科室的总数
        Connection conn = null;
        String sql = "";
        try {
            conn = DbControl.getConnection();

            sql = "select count(office_id) from office where display>0";
            totalCount = getQueryRunner().query(conn, sql,
                    new ScalarHandler<Long>());
            sql = "select * from office where display>0;";
            officeMapList = getQueryRunner().query(conn, sql,
                    new MapListHandler(new BasicRowProcessorFix()));
            logger.info("查询到的科室共有" + officeMapList.size() + "个");
        } catch (Exception e) {
            logger.error("", e);
        } finally {
            DbUtils.closeQuietly(conn);
        }
        dataMap.put("totalCount", totalCount);
        dataMap.put("dataList", officeMapList);
        return dataMap;
    }

    /**
     * 通过office_id获得相关诊室的详细信息
     * @param office_id  科室独有的ID号
     * @return 科室详细信息的Map
     */
    public Map<String,Object> getOfficeDetail(int office_id){
        logger.info("执行 getOfficeDetail");
        Map<String, Object> officeDetailMap = null;
        Connection conn = null;
        String sql="";
        try {
            conn = DbControl.getConnection();
            sql = "select * from office where office_id = ?";
            officeDetailMap = getQueryRunner().query(conn, sql,
                    new MapHandler(new BasicRowProcessorFix()),office_id);

            //查询结果判断
            if(officeDetailMap.size()>0)
                logger.info("已查询到相关信息");
            else
                logger.info("未查到相关信息或查询信息出错");

        } catch (Exception e) {
            logger.error("", e);
        } finally {
            DbUtils.closeQuietly(conn);
        }
        String loc="";
        List<String> loclist=null;
        //对得到的location进行切分,遍历key找到带_loc的字段

        Map<String, Object> tempofficeDetailMap = new HashMap<String, Object>();

        for (String key : officeDetailMap.keySet()) {
            if(key.contains("_loc")||key.equals("relevant_person")){
                loc = officeDetailMap.get(key).toString();
                logger.info("科室图片地址或相关人集合为:"+loc);
                if(loc!=null) {
                    loclist = spitLocation(loc);
                    officeDetailMap.replace(key, loclist);
                    tempofficeDetailMap.put(key+"_count", loclist.size());
                }
            }
        }

        for (String key : tempofficeDetailMap.keySet()) {
            officeDetailMap.put(key,tempofficeDetailMap.get(key));
        }
        return officeDetailMap;
    }

    /**
     * 切分混在一起的地址字符串
     * @param loc  地址
     * @return  地址的String数组
     */
    public List<String> spitLocation(String loc){

        String[] locationlist = loc.split(";");
        List<String> loclist = new ArrayList<String>();
//        logger.info("切分后得到地址：");
        for (int i = 0; i < locationlist.length; i++) {
//            logger.info(locationlist[i]);
            loclist.add(locationlist[i]);
        }
        return loclist;
    }
}
